package org.wsiz.vro.util;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;

/**
 * {@link AnnotationBeanNameGenerator} to remove the trailing <code>Impl</code> of the spring bean names that are picked by spring using the annotation scanning feature.
 * <p>
 * To enable in applicationContext:
 * <pre>
 *  &lt;context:component-scan base-package="org.wsiz.vro" name-generator="org.wsiz.vro.util.WithoutTrailingImplBeanNameGenerator"&gt;
 * </pre>
 * @see AnnotationBeanNameGenerator
 */
public class WithoutTrailingImplBeanNameGenerator extends AnnotationBeanNameGenerator {
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        String beanName = super.generateBeanName(definition, registry);
        if (!beanName.endsWith("Impl")) {
            return beanName;
        }
        return StringUtils.substringBeforeLast(beanName, "Impl");
    }
}
